const {createRole,findRoles,deleteARole,updateRoleInfo,findRolesWithinLimits} =  require('../controller/roleController')
const {Router} = require('express')

let router = new Router()


//根据req.query查询指定角色
router.get('/roles',(req,res)=>{
    findRoles(req.query).then((value)=>{
        res.json({
            code:200,
            message:"成功",
            data:value
        })
    },err=>{
        res.json({
            massage:err
        })
    })

})

//新增角色信息
router.post('/role',(req,res)=>{
    let result = createRole(req.body)

    result.then((value)=>{
        res.json({
            code:200,
            message:"成功",
            data:value
        })
    },(err)=>{
        res.json({
            message:err
        })
    })

})

//删除一个角色
router.delete('/role',(req,res)=>{
    deleteARole(req.body).then((value)=>{
        res.json({
            code:200,
            msg:value
        })
    },(error)=>{
        res.json({
            code:202,
            msg:error
        })
    })
})

//修改角色信息
router.put('/role',(req,res)=>{
    const {_id,updateInfo} = req.body

    updateRoleInfo(_id,updateInfo).then(value=>{
        res.json({
            code:200,
            msg:value
        })
    },reason=>{
        res.json({
            code:202,
            msg:reason
        })
    })
})

//按照pagination要求，查询指定角色信息
router.get('/roles/pagination',(req,res)=>{
    let {pageNo,pageSize} = req.query

    findRolesWithinLimits(pageNo,pageSize).then(
        value=>{
            res.json({
                code:200,
                data:value.roleInfoArr,
                total:value.total
            })
        },
        reason=>{
            res.json({
                code:202,
                msg:reason
            })
        }
    )
})
module.exports = function(){
    return router
}